/**
**********************************************************************
* @file     pixel_operation.c
* @brief    对文件的简要说明
* @details  对文件的详细说明
* @author   jcai
* @date     2022-05-07 09:37:57
* @copyright Copyright &copy; 2022 wy1107412334@163.com All rights reserved
* @version  v0.0.1
*
* \n Email:  wy1107412334@163.com
* \n Blog:   None
* \n Github: https://github.com/wy1107412334
* \n Gitee:  https://gitee.com/git.git1107412334.com
*
**********************************************************************
* @note
* None.
**********************************************************************
* @attention
* None.
**********************************************************************
@verbatim
@endverbatim
**********************************************************************
* @par 历史记录
* 1. v0.0.1: jcai, 2022-05-07 09:37:57
*   - 首次编写
*
*<center>Copyright &copy; 2022 wy1107412334@163.com All rights reserved</center>
**********************************************************************
*/

/* 头文件导入区 --------------------------------------------------- */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "pixel_operation.h"



void setPixel32_nolock(SDL_Surface * surface, uint32_t x, uint32_t y,
                       uint32_t color)
{
    uint8_t * pixel = (uint8_t *)surface->pixels;
    pixel += (y * surface->pitch) + (x * sizeof(uint32_t));
    *((uint32_t *)pixel) = color;
}

void setPixel24_nolock(SDL_Surface * surface, uint32_t x, uint32_t y,
                       uint32_t color)
{
    uint8_t * pixel = (uint8_t *)surface->pixels;
    pixel += (y * surface->pitch) + (x * sizeof(uint8_t) * 3);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    pixel[0] = (color >> 24) & 0xFF;
    pixel[1] = (color >> 16) & 0xFF;
    pixel[2] = (color >> 8) & 0xFF;
#else
    pixel[0] = color & 0xFF;
    pixel[1] = (color >> 8) & 0xFF;
    pixel[2] = (color >> 16) & 0xFF;
#endif
}

void setPixel16_nolock(SDL_Surface * surface, uint32_t x, uint32_t y,
                       uint16_t color)
{
    uint8_t * pixel = (uint8_t *)surface->pixels;
    pixel += (y * surface->pitch) + (x * sizeof(uint16_t));
    *((uint16_t *)pixel) = color;
}

void setPixel8_nolock(SDL_Surface * surface, uint32_t x, uint32_t y,
                      uint8_t color)
{
    uint8_t * pixel = (uint8_t *)surface->pixels;
    pixel += (y * surface->pitch) + (x * sizeof(uint8_t));
    *pixel = color;
}

int setPixel32(SDL_Surface * surface, uint32_t x, uint32_t y, uint32_t color)
{
    if ((x > (uint32_t) surface->w) || (y > (uint32_t) surface->h))
        return -1;

    if( SDL_MUSTLOCK(surface) )
        SDL_LockSurface(surface);
    setPixel32_nolock(surface, x, y, color);
    if( SDL_MUSTLOCK(surface) )
        SDL_UnlockSurface(surface);
    return 0;
}

int setPixel24(SDL_Surface * surface, uint32_t x, uint32_t y, uint32_t color)
{
    if ((x > (uint32_t) surface->w) || (y > (uint32_t) surface->h))
        return -1;

    if( SDL_MUSTLOCK(surface) )
        SDL_LockSurface(surface);
    setPixel24_nolock(surface, x, y, color);
    if( SDL_MUSTLOCK(surface) )
        SDL_LockSurface(surface);
    return 0;
}

int setPixel16(SDL_Surface * surface, uint32_t x, uint32_t y, uint16_t color)
{
    if ((x > (uint32_t) surface->w) || (y > (uint32_t) surface->h))
        return -1;

    if( SDL_MUSTLOCK(surface) )
        SDL_LockSurface(surface);
    setPixel16_nolock(surface, x, y, color);
    if( SDL_MUSTLOCK(surface) )
        SDL_UnlockSurface(surface);
    return 0;
}

int setPixel8(SDL_Surface * surface, uint32_t x, uint32_t y, uint8_t color)
{
    if ((x > (uint32_t) surface->w) || (y > (uint32_t) surface->h))
        return -1;

    if( SDL_MUSTLOCK(surface) )
        SDL_LockSurface(surface);
    setPixel8_nolock(surface, x, y, color);
    if( SDL_MUSTLOCK(surface) )
        SDL_UnlockSurface(surface);
    return 0;
}






/***  Copyright (C) 2022 wy1107412334@163.com All rights reserved ***/
/****************************** END OF FILE *************************/

